﻿using Engine;
using Engine.Graphics;
using Game;

namespace Mekiasm
{
    public class ElectricFoot : ClothingItem,Interface.IChargeAbleItem
    {
        public static int Index;

        public float FallResilience = 0f;

        public ComponentEnergyMachine.FuelType ChargeFuelType => ComponentEnergyMachine.FuelType.Electric;

        public long MaxAmount => GetMaxPower();

        public ElectricFoot() : base( "自由助跑器")
        {
            Clothing.MovementSpeedFactor = 1.2f;
            Clothing.ArmorProtection = 0.1f;
        }


        public override void ClothingEquipedUpdate(int value, ComponentPlayer componentPlayer)
        {
            SaveItemSystem.GetBlockData(value, out Common.SaveItem toolSaveItem);
            if (toolSaveItem != null && toolSaveItem.fuelAmount >= 10)
            {
                if (ILibrary.SubsystemTime.PeriodicGameTimeEvent(1.0, 0.0))
                {
                    SaveItemSystem.ChangePower(componentPlayer.ComponentMiner.Inventory, componentPlayer.ComponentMiner.Inventory.ActiveSlotIndex, -10);
                }
                if (FallResilience == 0f) FallResilience = componentPlayer.ComponentHealth.FallResilience;//缓存基础数值
                componentPlayer.ComponentHealth.FallResilience = 256f;
            }
            else
            {
                if (FallResilience != 0f) componentPlayer.ComponentHealth.FallResilience = FallResilience;
            }
            if (toolSaveItem == null || toolSaveItem.Power == 0)
            {
                Clothing.ArmorProtection = 0f;
                Clothing.MovementSpeedFactor = 1f;
            }
            else
            {
                Clothing.MovementSpeedFactor = 1.2f;
                Clothing.ArmorProtection = 0.1f;
            }
        }

        public override void ClothingUnEquiped(int value, ComponentPlayer componentPlayer)
        {
            base.ClothingUnEquiped(value, componentPlayer);
            componentPlayer.ComponentHealth.FallResilience = FallResilience;
        }

        public override int GetMaxPower()
        {
            return 10240;
        }
        
        public override bool IsCrushWhenNoPower()
        {
            return false;
        }

        public override bool IsClothing()
        {
            return true;
        }

        public override ClothingSlot GetClothingSlot()
        {
            return ClothingSlot.Feet;
        }

        public override Texture2D GetTexture()
        {
            return ContentManager.Get<Texture2D>("Mekiasm/Textures/Clothing/ElectricFoot");
        }

        public override float GetBlockHealth(int value)
        {
            SaveItemSystem.GetBlockData(value, out var d);
            if (d == null) return -1f;
            else return GetCurrentAmount(value) / (float)GetMaxPower();
        }


        public long GetCurrentAmount(int value)
        {
            SaveItemSystem.GetBlockData(value, out var d);
            if (d == null) return 0;
            else return d.fuelAmount;
        }

        public int SetCurrentAmount(int value, int fuelValue, long varAmount)
        {
            return SaveItemSystem.ChangePower(value, varAmount, fuelValue);
        }

        public int GetChargeFuelValue(int value)
        {
            return 0;
        }

        public bool CanAcceptFuel(int value)
        {
            return true;
        }
    }
}
